using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.Controls;
using WP.DB;
using WP.Modules;

using System.Data.SqlClient;

namespace WP.Modules
{
    /// <summary>
    /// 
    /// </summary>
    public partial class frmAddPK : BaseQryRpt
    {
        #region Constructor Functions

        public frmAddPK()
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            
            this.MinimumSize = this.Size;
        }
        private string mstrItemNo= string.Empty;

        public bool AddPK(string strItemNo)
        {
            this.Text = "Item#: " + strItemNo;
            #region Val
            string strSql = string.Empty;
            
            mstrItemNo = strItemNo;
            
            #endregion 
            
            
                this.ShowDialog();
            
            return true;
        }

        #endregion

        #region Private Events

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (AddNewPK())
            {
                this.Close();
            }
        }

        private bool AddNewPK()
        {
            Fields_Leave(txtPK, null);

            DBAccess dbcon = new DBAccess(false, false);
            try
            {

                dbcon.BeginTran();

                string strSql = string.Empty, strErrMsg=string.Empty ;

                string strUnit ="PK"+BaseFunctions.FormatWithZero( txtPK.Text.Trim() ,3);
                if (CvtFunc.ToDouble(txtPK.Text) >= 1000) strUnit = "P" + BaseFunctions.FormatWithZero(txtPK.Text.Trim() , 4);
    
                if (!ValidateCheck(dbcon, strUnit,ref strErrMsg))
                {
                    dbcon.RollbackTran();
                    MsgBoxEx.Show(strErrMsg, "Warning");

                    return false;
                }

                strSql=@"declare @intMaxNo int
                select @intMaxNo=max(SeqNo)+10 from imInvMasterUnit where itemno=@ItemNo
                insert into imInvMasterUnit(SeqNo,Unit,ItemNo) values(isnull(@intMaxNo,10),@Unit,@ItemNo) ";
                SqlParameter[] param ={
                    new SqlParameter("@ItemNo",mstrItemNo)
                    ,new SqlParameter("@Unit",strUnit)

                };
                dbcon.ExecuteSql(strSql, param);

                dbcon.CommitTran();

                
            }
            catch (Exception ex)
            {
                dbcon.RollbackTran();
                ExceptionManager.ProcessException(ex);
                return false;
            }
            finally
            {
                dbcon.CloseCon();
            }
            return true;
        }

        private bool ValidateCheck(DBAccess dbcon, string strUnit, ref string strErrMsg)
        {
            if (!DataFunctions.CheckRecordExist(dbcon,"iminvmaster",mstrItemNo,"ItemNo"))
            {
                strErrMsg =string.Format( Msg.Select("Item# {0} not on file!", DataFunctions.GetMsg(dbcon, this.Name, "MSG_ItemNotOnFile")),mstrItemNo);
                return false;
            }

            int intPK =CvtFunc.ToInt( CvtFunc.ToSubstring(strUnit, 2));
            if (intPK <= 0)
            {
                strErrMsg=Msg.Select("Pack Size must be greater than zero!",DataFunctions.GetMsg(dbcon,this.Name,"MSG_UnitGreateThanZero"));
                return false;
            }

            if (!DataFunctions.CheckRecordExist(dbcon, "imUnit", strUnit, "Unit"))
            {
                strErrMsg = string.Format(Msg.Select("Unit {0} not on file!", DataFunctions.GetMsg(dbcon, this.Name, "MSG_UnitNotOnFile")), strUnit);
                return false;
            }

            if (DataFunctions.CheckRecordExist(dbcon, "imInvMasterUnit", strUnit, "Unit","ItemNo='"+mstrItemNo+"'"))
            {
                strErrMsg = string.Format(Msg.Select("Unit {0} already on file for Item# {1}!", DataFunctions.GetMsg(dbcon, this.Name, "MSG_UnitOnFile")), strUnit,mstrItemNo);
                return false;
            }


            return true;
        }

        
        private void btnExit_Click(object sender, EventArgs e)
        {
            
            
                this.Close();
            
        }

        private void frmPoAddPK_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }        

        private void Fields_Leave(object sender, EventArgs e)
        {
            TextBox txt = sender as TextBox;

            switch (txt.Name)
            {
                case "txtPK":
                    if(txt.Text.Trim().Length <=3) 
                      txt.Text =CvtFunc.ToRightString( BaseFunctions.FormatNumeric(txt.Text, "n.0"),3);
                    else
                       txt.Text = CvtFunc.ToRightString(BaseFunctions.FormatNumeric(txt.Text, "n.0"), 4);
                    break;
                
            }
        }

        

        private void btnHelp_Click(object sender, EventArgs e)
        {
            base.OnHelp();
        }

        #endregion

        
    }
}